package com.genexususa.eo.bing.data.results;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.AnalyticsApiFaultDetail;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.BatchError;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.EditorialError;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.GoalError;
import com.genexususa.eo.bing.data.GxItemException;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Bing_GoalResults")
public class GxGoalResults extends GxGeneralResults {
    @XmlElementWrapper(name = "GoalResultCollection")
    @XmlElement(name = "GoalResult")
    public GxGoalResult[] goalResultCollection;

    public GxGoalResults() {
        super();
    }

    public GxGoalResults(String trackingId) {
        super(trackingId);
    }

    public GxGoalResults(String trackingId, int resultQuantity) {
        super(trackingId);
        this.goalResultCollection = new GxGoalResult[resultQuantity];
    }

    public void addResult(int index, GxGoalResult goalResult) {
        this.goalResultCollection[index] = goalResult;
    }

    public void addGxExceptionError(GxItemException error, int index) {
        this.goalResultCollection[index].addGxExceptionError(error, index);
    }

    public void addBatchError(BatchError error) {
        this.goalResultCollection[error.getIndex()].addBatchError(error);
    }

    public void addEditorialError(EditorialError error) {
        this.goalResultCollection[error.getIndex()].addEditorialError(error);
    }

    public void addAnalyticsApiFaultDetailError(AnalyticsApiFaultDetail fault) {
        this.hasError = true;
        GoalError[] errors = fault.getGoalErrors();
        for (int i=0; i<errors.length; i++) {
            this.goalResultCollection[errors[i].getIndex()].addGoalError(errors[i]);
        }
        this.errorCollection.addOperationErrors(fault.getOperationErrors());
        this.errorCollection.addApplicationFaultError(fault);
    }
}
